package framework.fastdevelop.com.touch.group;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.OverScroller;

public class RefreshLayout extends LinearLayout {

    private RefreshHeader refreshHeader;

    private int lastY;

    private OverScroller scroller;

    private RefreshState currState;

    static final String TAG = RefreshLayout.class.getSimpleName();

    public enum RefreshState{
        Pulling,//下拉中
        Release,//松手刷新
        Refreshing,//刷新中
        Success,//刷新成功
    }

    public RefreshLayout(Context context) {
        this(context, null);
    }

    public RefreshLayout(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public RefreshLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initRefresh(context);
    }

    private void initRefresh(final Context context) {
        setOrientation(LinearLayout.VERTICAL);
        refreshHeader = new RefreshHeader(context);
        addView(refreshHeader);

        setY(-refreshHeader.getDefaultHeight());

        scroller = new OverScroller(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                lastY = (int) event.getY();
                if (!scroller.isFinished()) {
                    scroller.abortAnimation();
                }
                break;
            case MotionEvent.ACTION_MOVE:

                int dy = lastY - (int) event.getY();
                scrollBy(0, (int)((float)dy * 0.7f));
                lastY = (int) event.getY();
                if(getScrollY() <= -refreshHeader.getDefaultHeight()){
                    refreshHeader.refreshState(RefreshState.Release);
                    currState = RefreshState.Release;
                }else{
                    refreshHeader.refreshState(RefreshState.Pulling);
                    currState = RefreshState.Pulling;
                }

                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                int upDy = isUp() ? Math.abs(getScrollY()) : -getScrollY();
                scroller.startScroll(0, getScrollY(), 0, upDy, 500);
                postInvalidate();

                if(RefreshState.Release.equals(currState)){
                    refreshHeader.refreshState(RefreshState.Refreshing);
                    scroller.startScroll(0,getScrollY(),0,Math.abs(getScrollY()) - Math.abs(refreshHeader.getDefaultHeight()),500);
                    postInvalidate();
                    currState = RefreshState.Refreshing;
                }
                break;
        }
        return true;
    }

    private void printLOG(String content){
        Log.i(TAG,content);
    }

    private boolean isUp(){
        return getScrollY() <= 0;
    }
    @Override
    public void computeScroll() {
        if (scroller.computeScrollOffset()) {
            scrollTo(scroller.getCurrX(), scroller.getCurrY());
            postInvalidate();
        }
    }
}
